/**
 *
 *      ___                       ___
 *     /\  \          ___        /\  \
 *    /::\  \        /\  \      /::\  \
 *   /:/\:\  \       \:\  \    /:/\ \  \
 *  /:/  \:\  \      /::\__\  _\:\~\ \  \
 * /:/__/ \:\__\  __/:/\/__/ /\ \:\ \ \__\
 * \:\  \  \/__/ /\/:/  /    \:\ \:\ \/__/
 *  \:\  \       \::/__/      \:\ \:\__\
 *   \:\  \       \:\__\       \:\/:/  /
 *    \:\__\       \/__/        \::/  /
 *     \/__/                     \/__/
 *
 * CIS - Clang Indexing Service is a Project Manager & Source code indexing service
 * created so as to simplify the indexing of C/C++ based source code which use
 * standardized build mechanisms.
 *
 **/

#include "../includes/project.hxx"
#include "../includes/projectdirectory.hxx"

#include <Poco/Logger.h>
#include <Poco/LogStream.h>
#include <Poco/File.h>
#include <Poco/Util/Application.h>
#include <Poco/Path.h>
#include <Poco/Exception.h>
#include <fstream>

using namespace Poco::Util;
using namespace cis;
using Poco::File;
using Poco::Path;

namespace {
Poco::Logger& logger = Poco::Logger::get("Project");
Poco::LogStream logstream(logger);
}

Project::Project(const std::string& name, const std::string& mf, const std::string makeparams) : name(name), makefile(mf), makeparams(makeparams)
{
    this->buildpath = Application::instance().config().getString("projects.basedir")
                      + Path::separator() + name + Path::separator();
    Path buildPath;
    buildPath.parse(buildpath);
    File projDir(buildPath);
    try {
        if (projDir.exists()) {
            logstream << "project directory exists, will remove - " << buildPath.toString() << std::endl;
            projDir.remove(true);
        }
        projDir.createDirectories();
    } catch (const Poco::Exception& e) {
        logstream << "couldn't create the project directory - " << buildPath.toString() << std::endl;
        throw Poco::Exception("couldn't create the project directory - " + buildPath.toString());
    }
    logstream << "instantiated project [" << name << ", " << mf << ", " << makeparams << "] @ " << buildPath.toString() << std::endl;
    projectStream.open(std::string(buildpath + "/project.log").c_str());
    projectStream.rdbuf()->pubsetbuf(0, 0);
    compilationDB = Poco::SharedPtr<CompilationDB > (new CompilationDB(*this));
}

Project::~Project()
{
    projectStream.close();
}

void Project::build()
{
    compilationDB->build();
}

bool Project::hasCompilationErrors() const
{
    return false;
}

void Project::getDiagnostics()
{

}

void Project::getCompletionsAtLoc(const Location& loc)
{

}

void Project::getSymbolReferences(const Location& loc)
{

}

void Project::getAllSymbolReferences(const Location& loc)
{

}

void Project::getFileOutline(const std::string& file)
{

}

void Project::getOutline(const std::string& file)
{

}

void Project::getOutline()
{

}

void Project::getDeclaration()
{

}

void Project::getDefination()
{

}
